package sv.gob.mh.dgii.ssc.controller.configuration;

import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;

public class BasicViewForm implements Serializable {

    protected BasicTableView table = new BasicTableView();
    protected Date fechaInicial;
    protected Date fechaFinal;
    protected List<Exception> errores = new ArrayList<Exception>();
    private int clickedTab = 1;
    private String formName;
    private String prefix;

    public BasicViewForm() {
    }

    public BasicViewForm(String prefix, String formName) {
        this.formName = formName;
        this.prefix = prefix;
    }

    public String getErrorMessages() {
        String ret = null;
        if (errores != null && errores.size() > 0) {
            /*ui-state-error ui-corner-all*/
            ret = " <div id=\"errorMessages\" class=\"ui-widget\"><div class=\"ui-state-highlight ui-corner-all\" style=\" padding: 10px;\">";
            ret += "<h1 class=\"error\"><span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: .3em;\">&nbsp;</span>Advertencia</h1>";
            ret += "<ul class=\"error\">";
            for (Exception e : errores) {
                String error = "<span class=\"errorMessageItem\">" + e.getMessage() + "</span>";
                ret += "<li>" + error + "<span style=\"display:none;\"><br />" + getStackTrace(e).replace("\"", "\'") + "</span></li>";
            }
            ret += "</ul>";
            ret += "</div></div>";
        }
        errores.clear();
        return ret;
    }

    public String getSimpleErrorMessages() {
        String ret = null;
        if (errores != null && errores.size() > 0) {
            /*ui-state-error ui-corner-all*/
            ret = "<ul class=\"error\">";
            for (Exception e : errores) {
                String error = "<span class=\"errorMessageItem\">" + e.getMessage() + "</span>";
                ret += "<li>" + error + "</li>";
            }
            ret += "</ul>";
        }
        return ret;
    }

    public void setPageData(String prefix, String formName) {
        setPrefix(prefix);
        setFormName(formName);
    }

    public void addError(Log log, Exception e) {
        errores.add(e);
        log.debug(e);
    }

    public void addError(Log log, String error) {
        addError(log, new Exception(error));
    }

    public void clearErrores() {
        if (errores == null) {
            errores = new ArrayList<Exception>();
        }
        errores.clear();
    }

    public String getStackTrace(Exception e) {
        StringWriter out = new StringWriter();
        e.printStackTrace(new PrintWriter(out));
        return out.toString();
    }

    public String clickTab(int val) {
        String ret = "redirect:";
        setClicked(val);
        return ret;
    }

    public void setOrderByPaginator() {
        setOrderByPaginator(getTable());
    }

    public void setOrderByPaginator(BasicTableView tableView) {
        if (tableView != null) {
            tableView.setOrderByPaginator();
        }
    }

    public void paginate(BasicTableView tableView, Integer page, String sortProperty, Integer switchOrder) {
        tableView.paginate(page, sortProperty, switchOrder);
    }

    public void paginate(Integer page, String sortProperty, Integer switchOrder) {
        paginate(getTable(), page, sortProperty, switchOrder);
    }

    public long getPaginationRes() {
        return getPaginationRes(getTable());
    }

    public void clearListado() {
        clearListado(getTable());
    }

    public void clearListado(BasicTableView tableView) {
        List list = getListado(tableView);
        if (list != null) {
            getListado(tableView).clear();
            tableView.setAllCounter(0);
        }
    }

    public List getListado() {
        return getListado(getTable());
    }

    public List getListado(BasicTableView tableView) {
        List ret = null;
        if (tableView != null) {
            ret = tableView.getListado();
        }
        return ret;
    }

    public void setListado(List list) {
        setListado(getTable(), list);
    }

    public void setListado(BasicTableView tableView, List list) {
        if (tableView != null) {
            tableView.setListado(list);
        }
    }

    public Long getPaginationRes(BasicTableView tableView) {
        Long ret = null;
        if (tableView != null) {
            ret = tableView.getPaginationRes();
        }
        return ret;
    }

    public String getDirection() {
        return getDirection(getTable());
    }

    public String getDirection(BasicTableView tableView) {
        String ret = null;
        if (tableView != null) {
            ret = tableView.getDirection();
        }
        return ret;
    }

    public void setDirection(String direction) {
        getTable().setDirection(direction);
    }

    public void setDirection(BasicTableView tableView, String direction) {
        if (tableView != null) {
            tableView.setDirection(direction);
        }
    }

    public String getSortProperty() {
        return getSortProperty(getTable());
    }

    public String getSortProperty(BasicTableView tableView) {
        String ret = null;
        if (tableView != null) {
            ret = tableView.getSortProperty();
        }
        return ret;
    }

    public void setSortProperty(String sortProperty) {
        setSortProperty(getTable(), sortProperty);
    }

    public void setSortProperty(BasicTableView tableView, String sortProperty) {
        if (tableView != null) {
            tableView.setSortProperty(sortProperty);
        }
    }

    public int getRecordsToView() {
        return getRecordsToView(getTable());
    }

    public Integer getRecordsToView(BasicTableView tableView) {
        Integer ret = null;
        if (tableView != null) {
            ret = tableView.getRecordsToView();
        }
        return ret;
    }

    public void setRecordsToView(int recordsToView) {
        getTable().setRecordsToView(recordsToView);
    }

    public void setRecordsToView(BasicTableView tableView, int recordsToView) {
        if (tableView != null) {
            tableView.setRecordsToView(recordsToView);
        }
    }

    public Integer getCurrentPage() {
        return getCurrentPage(getTable());
    }

    public Integer getCurrentPage(BasicTableView tableView) {
        Integer ret = null;
        if (tableView != null) {
            ret = tableView.getCurrentPage();
        }
        return ret;
    }

    public void setCurrentPage(int currentPage) {
        getTable().setCurrentPage(currentPage);
    }

    public void setCurrentPage(BasicTableView tableView, int currentPage) {
        if (tableView != null) {
            tableView.setCurrentPage(currentPage);
        }
    }

    public Integer getAllCounter() {
        return getAllCounter(getTable());
    }

    public Integer getAllCounter(BasicTableView tableView) {
        Integer ret = null;
        if (tableView != null) {
            ret = tableView.getAllCounter();
        }
        return ret;
    }

    public void setAllCounter(int AllCounter) {
        getTable().setAllCounter(AllCounter);
    }

    public void setAllCounter(BasicTableView tableView, int AllCounter) {
        if (tableView != null) {
            tableView.setAllCounter(AllCounter);
        }
    }

    /**
     * @return the fechaInicial
     */
    public Date getFechaInicial() {
        return fechaInicial;
    }

    /**
     * @param fechaInicial the fechaInicial to set
     */
    public void setFechaInicial(Date fechaInicial) {
        this.fechaInicial = fechaInicial;
    }

    /**
     * @return the fechaFinal
     */
    public Date getFechaFinal() {
        return fechaFinal;
    }

    /**
     * @param fechaFinal the fechaFinal to set
     */
    public void setFechaFinal(Date fechaFinal) {
        this.fechaFinal = fechaFinal;
    }

    /**
     * @return the clickedTab
     */
    public int getClicked() {
        return clickedTab;
    }

    /**
     * @param clickedTab the clickedTab to set
     */
    public void setClicked(int clicked) {
        this.clickedTab = clicked;
    }

    /**
     * @return the table
     */
    public BasicTableView getTable() {
        if (table == null) {
            table = new BasicTableView();
        }
        return table;
    }

    /**
     * @param table the table to set
     */
    public void setTable(BasicTableView table) {
        this.table = table;
    }

    /**
     * @return the errores
     */
    public List<Exception> getErrores() {
        if (errores == null) {
            errores = new ArrayList<Exception>();
        }
        return errores;
    }

    /**
     * @param errores the errores to set
     */
    public void setErrores(List<Exception> errores) {
        this.errores = errores;
    }

    /**
     * @return the formName
     */
    public String getFormName() {
        return formName;
    }

    /**
     * @param formName the formName to set
     */
    public void setFormName(String formName) {
        this.formName = formName;
    }

    /**
     * @return the prefix
     */
    public String getPrefix() {
        return prefix;
    }

    /**
     * @param prefix the prefix to set
     */
    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

}
